<?php

namespace App\Http\Controllers\Admin;

use App\Models\Packorder;
use App\Models\Store;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PackorderController extends Controller
{

    public function __construct()
    {
        view()->share([
            '_orders' => 'am-in',
            '_packorder' => 'am-active',
        ]);
    }

//挑战订单管理
    public function index(Request $request)
    {
        $where = function ($query) use ($request) {
            //按订单号
            if ($request->has('order_no') and $request->order_no != '') {
                $search = "%" . $request->order_no . "%";
                $query->where('order_no', 'like', $search);
            }
            //按会员手机号
            if ($request->has('oncustomer_tel') and $request->oncustomer_tel != '') {
                $search = "%" . $request->oncustomer_tel . "%";
                $query->where('oncustomer_tel', 'like', $search);
            }
           //按会员姓名
            if ($request->has('oncustomer_name') and $request->oncustomer_name != '') {
                $search = "%" . $request->oncustomer_name . "%";
                $query->where('oncustomer_name', 'like', $search);
            }
            //按门店
            if ($request->has('store_id') and $request->store_id != '-1') {

                $query->where('store_id', $request->store_id);
            }


        };
        //按门店
        $cates = Store::all();
        //挑战订单关联会员，门店，挑战项目
        $packorders = Packorder::where($where)->with('oncustomer')->with('store')->with('pack')->orderBy('id', 'desc')->get();


        return view('admin.order.packorder', compact('packorders','cates'));

    }
}
